package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.Employeeinfo;
import cn.wolfcode.crm.mapper.EmployeeInfoMapper;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IEmployeeinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EmployeeinfoServiceImpl implements IEmployeeinfoService {
    @Autowired
    private EmployeeInfoMapper employeeInfoMapper;

    @Override
    public Map selectData() {
        Map map = new HashMap();
        List employeeInfos = employeeInfoMapper.selectAll();
        int ageNumber1 = 0;
        int ageNumber2 = 0;
        int ageNumber3 = 0;
        int ageNumber4 = 0;
        int ageNumber5 = 0;
        Iterator iterator = employeeInfos.iterator();
        do {
            if (!iterator.hasNext())
                break;
            Employeeinfo employeeInfo = (Employeeinfo) iterator.next();
            if (employeeInfo.getAge().intValue() > 0)
                if (employeeInfo.getAge().intValue() <= 20)
                    ageNumber1++;
                else if (employeeInfo.getAge().intValue() > 20 && employeeInfo.getAge().intValue() <= 30)
                    ageNumber2++;
                else if (employeeInfo.getAge().intValue() > 30 && employeeInfo.getAge().intValue() <= 40)
                    ageNumber3++;
                else if (employeeInfo.getAge().intValue() > 40 && employeeInfo.getAge().intValue() <= 50)
                    ageNumber4++;
                else
                    ageNumber5++;
        } while (true);
        int ageData[] = {
                ageNumber1, ageNumber2, ageNumber3, ageNumber4, ageNumber5
        };
        String age[] = {
                "20岁以下", "20~30岁", "30~40岁", "40~50岁", "50岁以上"
        };
        map.put("age", age);
        map.put("ageData", ageData);
        map.put("totalNumber", Integer.valueOf(employeeInfos.size()));
        List map2 = employeeInfoMapper.selectGenderData();
        Map genMap1 = (Map) map2.get(0);
        genMap1.put("gender", "男");
        Map genMap2 = (Map) map2.get(1);
        genMap2.put("gender", "女");
        List genderData = new ArrayList();
        Long genderDataMax = Long.valueOf(0L);
        Map genderMap;
        for (Iterator iterator1 = map2.iterator(); iterator1.hasNext(); genderData.add(genderMap)) {
            Map objectMap = (Map) iterator1.next();
            genderMap = new HashMap();
            Long genderData1 = (Long) objectMap.get("genderData");
            if (genderData1.longValue() > genderDataMax.longValue())
                genderDataMax = genderData1;
            genderMap.put("name", objectMap.get("gender"));
            genderMap.put("value", genderData1);
        }

        map.put("gender", new String[]{
                "男", "女"
        });
        map.put("genderData", genderData);
        map.put("genderDataMax", genderDataMax);
        List map1 = employeeInfoMapper.selectEducationData();
        Map eduMap1 = (Map) map1.get(0);
        eduMap1.put("education", "本科");
        Map eduMap2 = (Map) map1.get(1);
        eduMap2.put("education", "大专");
        List educationData = new ArrayList();
        Long educationDataMax = Long.valueOf(0L);
        Map educationMap;
        for (Iterator iterator2 = map1.iterator(); iterator2.hasNext(); educationData.add(educationMap)) {
            Map objectMap = (Map) iterator2.next();
            educationMap = new HashMap();
            Long educationData1 = (Long) objectMap.get("educationData");
            if (educationData1.longValue() > educationDataMax.longValue())
                educationDataMax = educationData1;
            educationMap.put("name", objectMap.get("education"));
            educationMap.put("value", educationData1);
        }

        map.put("education", new String[]{
                "本科", "大专"
        });
        map.put("educationData", educationData);
        map.put("educationDataMax", educationDataMax);
        return map;
    }


    @Override
    public void delete(Long id) {
        employeeInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Employeeinfo entity) {
        if (entity.getId() != null) {
            employeeInfoMapper.updateByPrimaryKey(entity);
        } else {
            employeeInfoMapper.insert(entity);
        }
    }

    @Override
    public PageResult queryForList(QueryObject qo) {
        int rows = employeeInfoMapper.rows(qo);
        if (rows == 0) {
            return PageResult.EMPTY_RESULT;
        }
        PageResult result = new PageResult(rows, employeeInfoMapper.queryForList(qo));
        return result;
    }
}

